// pages/commissionFlow/commissionFlow.js

var app = getApp();
var page = 1;
var selectedNews = false;
var newsList = [];

Page({
  data:{
    addresslist: [],
    SwitchTabList: [
      {
        id: 1,
        title: '邀请佣金',
      },
      {
        id: 2,
        title: '额外佣金',
      }
    ],
    SwitchSelect: 1,
  },

  // 头部 Tab 切换
  switchList: function (e) {
    this.setData({
      SwitchSelect: e.currentTarget.dataset.id
    })
    page = 1;
    selectedNews = false;
    newsList = [];
    this.setData({
      addresslist: []
    })
    if (e.currentTarget.dataset.id == 1) {
      this.getOrdersList();
    } else {
      this.getCardsList();
    }
  },
  //获取邀请佣金列表
  getOrdersList: function () {
    var that = this;
    var url = '/wallet_api/cardInviteCommission';
    var params = {
      uid: app.globalData.userInfo.id,
      pageNo:page,
      pageSize:20
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        selectedNews = false;
        that.setData({
          addresslist: []
        })
      } else {
        var cardlist = res.data;
        for (var i = 0; i < cardlist.length; i++) {
          if (cardlist[i].status == 2) {
            cardlist[i].status = '已到账';
          } else {
            cardlist[i].status = '未到账';
          }
          cardlist[i].create_time = that.timestampToTime(cardlist[i].create_time * 1000);
          cardlist[i].date = cardlist[i].create_time.split('-')[0];
          cardlist[i].time = cardlist[i].create_time.split('-')[1];
          cardlist[i].money = cardlist[i].price;
          newsList.push(cardlist[i]);
        }
        if (cardlist.length < 20) {
          selectedNews = false;
        } else {
          selectedNews = true;
          page += 1;
        }
        that.setData({
          addresslist: newsList
        });
        wx.hideLoading();
      }
    });
  },
  //获取额外佣金列表
  getCardsList: function () {
    var that = this;
    var url = '/wallet_api/cardCommission';
    var params = {
      uid: app.globalData.userInfo.id,
      pageNo: page,
      pageSize: 20
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        var cardlist = res.data;
        for (var i = 0; i < cardlist.length; i++) {
          if (cardlist[i].status == 2){
            cardlist[i].status = '已到账';
          }else{
            cardlist[i].status = '未到账';
          }
          cardlist[i].create_time = that.timestampToTime(cardlist[i].create_time * 1000);
          cardlist[i].date = cardlist[i].create_time.split('-')[0];
          cardlist[i].time = cardlist[i].create_time.split('-')[1];
          newsList.push(cardlist[i]);
        }
        if (cardlist.length < 20) {
          selectedNews = false;
        } else {
          selectedNews = true;
          page += 1;
        }
        that.setData({
          addresslist: newsList
        });
        wx.hideLoading();
      }
    });
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    page = 1;
    selectedNews = false;
    newsList = [];
    this.getOrdersList();    
  },

  timestampToTime: function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '/';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var h = date.getHours() + ':';
    if (String(date.getMinutes()).length == 1) {
      var m = "0" + date.getMinutes() + ':';
    }else{
      m = date.getMinutes() + ':';
    }
    if (String(date.getSeconds()).length == 1) {
      var s = "0" + date.getSeconds() + ':';
    } else {
      s = date.getSeconds();
    }
    return Y + M + D + "-" + h + m + s;
  },//转化后格式xxxx-xx-xx,

  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示

  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  // onShareAppMessage: function () {
  //   return {
  //     title: '万商',
  //     desc: '你要的名片代',
  //     path: '/pages/home/home'
  //   }
  // },
  /** 
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {
    if (selectedNews == true){
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      
      if (that.data.SwitchSelect == 1){
        this.getOrdersList();
      } else {
        this.getCardsList();
      }

    }else{
      wx.hideLoading();
    }
  }
})